package com.gsbelarus.gedemin.salary.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;

import com.gsbelarus.gedemin.lib.sync.protocol.entity.SyncServiceStatus;
import com.gsbelarus.gedemin.lib.ui.activity.SyncActivityModel;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.entity.model.GdMsgModel;
import com.gsbelarus.gedemin.salary.fragment.gdmsg.GdMsgNewHolidayFragment;
import com.gsbelarus.gedemin.salary.fragment.gdmsg.GdMsgNewIncomeFragment;
import com.gsbelarus.gedemin.salary.fragment.gdmsg.GdMsgNewVacationFragment;
import com.gsbelarus.gedemin.salary.util.GCMHelper;

public class GdMsgNew extends SyncActivityModel {

    @Override
    public int getViewResource() {
        return R.layout.gdmsg_new_activity;
    }

    @Override
    public void onReceiveSyncStatus(SyncServiceStatus syncServiceStatus, SyncStatusDialog syncStatusDialog) {
        super.onReceiveSyncStatus(syncServiceStatus, syncStatusDialog);
        syncStatusDialog.show(syncServiceStatus);
    }

    @Override
    public void onCreateActivity(Bundle savedInstanceState) {
        super.onCreateActivity(savedInstanceState);

        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        GdMsgModel.Subject msgType = (GdMsgModel.Subject) getIntent().getSerializableExtra("gdMsgType");

        if (msgType != null)
            switch (msgType) {
                case INCOME:
                    openFragment(new GdMsgNewIncomeFragment());
                    break;
                case VACATION:
                    openFragment(new GdMsgNewVacationFragment());
                    break;
                case HOLIDAY:
                    openFragment(new GdMsgNewHolidayFragment());
                    break;
            }
    }

    private void openFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.replace(R.id.mail_content, fragment);
        fragmentTransaction.commitAllowingStateLoss();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.gdmsg_new, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.action_settings:
                Intent settingIntent = new Intent(GdMsgNew.this, Settings.class);
                startActivity(settingIntent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new GCMHelper(this).checkPlayServices(this);
    }
}
